﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CommonUtils;
using TERP.WaterClient.Entities.Auth;
using TERP.WaterClient.Common;
//using TERP.WaterClient.Common

namespace TERP.WaterClient.DbServices
{
	public class UserService : BaseService
	{
		public UserService()
			: base()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="grpCollection"></param>
		public void SaveUser(DataSet dsUser)
		{
			string strTId = SystemGlobal.Instance.GetNextTid();
			//
			try
			{
				DataSet mdsFinal = _dbService.Ser_SysSaveUser(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId,
					dsUser
				);
				//
                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
				if (CMyDataSet.HasError(mdsFinal))
				{
					throw GenServiceException(mdsFinal);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

        /// <summary>
        /// GetCurrentUser
        /// </summary>
        public User GetCurrentUser()
        {
            User user;
            string strTId = SystemGlobal.Instance.GetNextTid();
            //
            try
            {
                DataSet mdsFinal = _dbService.Ser_SysGetCurentUser(
                    _strGwUserCode, 
                    _strGwPassword,
                    strTId,
                    _strSessionId
                );

                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
                //
                if (CMyDataSet.HasError(mdsFinal))
                {
                    throw GenServiceException(mdsFinal);
                }
                DataTable dtblUser = mdsFinal.Tables[DbTable.Tbl_User];
                if (dtblUser.Rows.Count != 1)
                    throw new Exception(ErrorMessage.MSG_INVALID_USER);
                DataRow dtRow = dtblUser.Rows[0];

                user = new User();
                //user.DealerCode = Convert.ToString(dtRow[TblUser.Dealer_Code]);
                user.UserCode = Convert.ToString(dtRow[TblUser.User_Code]);
                user.UserName = Convert.ToString(dtRow[TblUser.User_Name]);
                user.FlagActive = Convert.ToString(dtRow[TblUser.Flag_Active]);
                user.UserPassword = Convert.ToString(dtRow[TblUser.User_Password]);
                //user.FlagSysAdmin = Convert.ToString(dtRow[TblUser.Flag_Sys_Admin]);

                return user;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public FunctionCollection GetFunctionForUser(string userCode, string partnerCode)
        {
            string tlbMapUsrObj = "Map_SU_SO";
            try
            {
                string strTId = SystemGlobal.Instance.GetNextTid();
                DataSet mdsMapUsrObj = _dbService.Ser_SysGetMapSysUserSysObject(
                    _strGwUserCode, _strGwPassword,
                    strTId, _strSessionId
                    , userCode			//user code list
                    , ""            //user name pattern
                    , ""            //flag active list for user
                    , ""			//BU code list
                    , ""			//BU code pattern
                    , ""			//active list for dealer
                    , ""			//grp code list
                    , partnerCode	//partner code list
                    , ""			//obj code list
                    , ""			//obj type list
                    , ""			//flag active obj list
                );
                mdsMapUsrObj = TERP.Utils.CUtils.MyDSDecode(mdsMapUsrObj);
                if (CMyDataSet.HasError(mdsMapUsrObj))
                {
                    throw GenServiceException(mdsMapUsrObj);
                }
                //
                DataTable dtbUsrObj = mdsMapUsrObj.Tables[tlbMapUsrObj];
                string prefixSO = "SO";
                //
                FunctionCollection objCollection = new FunctionCollection();
                foreach (DataRow dtRowObj in dtbUsrObj.Rows)
                {
                    FunctionObject functionObj = new FunctionObject();
                    functionObj.ObjectCode = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code]);
                    //functionObj.ObjectCodeParent = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code_Parent]);
                    functionObj.ObjectType = Convert.ToString(dtRowObj[TblFunctionObject.Object_Type]);
                    functionObj.PartnerCode = Convert.ToString(dtRowObj[TblFunctionObject.Partner_Code]);
                    //functionObj.ObjectCodeExec = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code_Exec]);
                    functionObj.FlagActive = Convert.ToString(dtRowObj[prefixSO + TblFunctionObject.Flag_Active]);
                    //
                    objCollection.Add(functionObj);
                }
                objCollection.FillToDataTable();
                return objCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

		/// <summary>
		/// 
		/// </summary>
		public UserCollection GetUser()
		{
			string tlbMapGrpUsr = "Map_SG_SU";
			string tlbMapUsrObj = "Map_SU_SO";
			//
			string prefixSU = "SU";
			//
			try
			{
				string strTId = SystemGlobal.Instance.GetNextTid();
				DataSet mdsFinalUsr = _dbService.Ser_SysGetUser(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId
					, ""			//user code list
					, ""			//user code pattern
					, Constants.Flag.Active			//flag active list
					, ""			//area code list
					, ""			//BU code list
					, ""			//BU code pattern
					, ""			//flag dealer active list
					, ""			//group code list
				);
				//
                mdsFinalUsr = TERP.Utils.CUtils.MyDSDecode(mdsFinalUsr);
				if (CMyDataSet.HasError(mdsFinalUsr))
				{
					throw GenServiceException(mdsFinalUsr);
				}
				//
				strTId = SystemGlobal.Instance.GetNextTid();
				DataSet mdsFinalMap= _dbService.Ser_SysGetMapSysGroupSysUser(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId
					, ""			//group code list
					, ""			//user code list
					, Constants.Flag.Active			//flag active list User
				);
				//
                mdsFinalMap = TERP.Utils.CUtils.MyDSDecode(mdsFinalMap);
				if (CMyDataSet.HasError(mdsFinalMap))
				{
					throw GenServiceException(mdsFinalMap);
				}
				//
				strTId = SystemGlobal.Instance.GetNextTid();
				DataSet mdsFinalObj = null;
				mdsFinalObj = _dbService.Ser_SysGetMapSysUserSysObject(
					_strGwUserCode, _strGwPassword,
					strTId, _strSessionId
					, ""			//user code list
                    //, ""			//dealer code list
					, ""			//user name pattern
                    //, ""			//flag sys admin list
					, Constants.Flag.Active			//flag active list user
					, ""			//BU code list
					, ""			//BU code pattern
					, ""			//active list for dealer
					, ""			//grp code list
					, ""			//partner code list
					, ""			//obj code list
					, ""			//obj type list
					, Constants.Flag.Active			//flag active obj list
				);
                mdsFinalObj = TERP.Utils.CUtils.MyDSDecode(mdsFinalObj);
				if (CMyDataSet.HasError(mdsFinalObj))
				{
					throw GenServiceException(mdsFinalObj);
				}
				UserCollection userCollection = new UserCollection();
				//
				DataTable dtbUsr = mdsFinalUsr.Tables[DbTable.Tbl_User];
				DataTable dtbMapGrpUser = mdsFinalMap.Tables[tlbMapGrpUsr];
				DataTable dtbUsrObj = mdsFinalObj.Tables[tlbMapUsrObj];
				//
				foreach (DataRow dtRowUsr in dtbUsr.Rows)
				{
					User user = new User();
					user.UserCode = Convert.ToString(dtRowUsr[TblUser.User_Code]);
					user.UserName = Convert.ToString(dtRowUsr[TblUser.User_Name]);
					user.UserPassword = Convert.ToString(dtRowUsr[TblUser.User_Password]);
					user.FlagActive = Convert.ToString(dtRowUsr[prefixSU + TblUser.Flag_Active]);
                    //user.FlagSysAdmin = Convert.ToString(dtRowUsr[TblUser.Flag_Sys_Admin]);
                    //user.DealerCode = Convert.ToString(dtRowUsr[TblUser.Dealer_Code]);
					//query group contain
					string tmpUserCode = Convert.ToString(dtRowUsr[TblUser.User_Code]);
					DataRow[] arrRowGrp = dtbMapGrpUser.Select(String.Format(TblUser.User_Code + "='{0}'", tmpUserCode));
					//
					GroupCollection grpCollection = new GroupCollection();
					foreach (DataRow dtRowGrp in arrRowGrp)
					{
						Group grp = new Group();
						grp.GroupCode = Convert.ToString(dtRowGrp[TblGroup.Group_Code]);
						grp.GroupName = Convert.ToString(dtRowGrp[TblGroup.Group_Name]);
						//
						grpCollection.Add(grp);
					}
					grpCollection.FillToDataTable();
					//////////////////////////////////////////////////////////////////////////
					//query obj belong
					DataRow[] arrRowObj = dtbUsrObj.Select(String.Format(TblUser.User_Code + "='{0}'", tmpUserCode));
					string prefixSO = "SO";
					//
					FunctionCollection objCollection = new FunctionCollection();
					foreach (DataRow dtRowObj in arrRowObj)
					{
						FunctionObject functionObj = new FunctionObject();
						functionObj.ObjectCode = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code]);
                        //functionObj.ObjectCodeParent = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code_Parent]);
						functionObj.ObjectType = Convert.ToString(dtRowObj[TblFunctionObject.Object_Type]);
						functionObj.PartnerCode = Convert.ToString(dtRowObj[TblFunctionObject.Partner_Code]);
                        //functionObj.ObjectCodeExec = Convert.ToString(dtRowObj[TblFunctionObject.Object_Code_Exec]);
						functionObj.ObjectName = Convert.ToString(dtRowObj[TblFunctionObject.Object_Name]);
						functionObj.FlagActive = Convert.ToString(dtRowObj[prefixSO + TblFunctionObject.Flag_Active]);
						//
						objCollection.Add(functionObj);
					}
					objCollection.FillToDataTable();
					//
					user.ListGroupContain = grpCollection;
					user.ListFunction = objCollection;
					userCollection.Add(user);
				}
				userCollection.FillToDataTable();
				return userCollection;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
